﻿#include <QFile>
#include "notepadio.h"
#include <QDebug>
#include <QTextStream>

NotepadIO::NotepadIO( QObject* parent ): QObject( parent )
{

}

bool NotepadIO::writeFile( const QByteArray& text,
                           const QUrl& url )
{
    QFile file( url.toLocalFile( ) );
    if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
    {
        file.write( text );
        file.close( );
        return true;
    }
    return false;
}

bool NotepadIO::writeFileStr(const QString &text, const QUrl &url)
{
    QFile file(url.toLocalFile() );

    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
        qDebug()<<"打开文件失败："<<url;
        return false;
    }

    QTextStream out(&file);

    out<<text;
    out.flush();

    file.close();

    return true;
}

QByteArray NotepadIO::readFile(const QUrl &url)
{
    QFile file( url.toLocalFile( ) );
    QByteArray data;
    if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
    {
        data = file.readAll();
        file.close( );
        return data;
    }
    return data;
}

NotepadIO* NotepadIO::instance( void )
{
    static NotepadIO io;
    return &io;
}
